// 有序数组的合并

let arr1 = [1, 3, 5, 6, 7]
let arr2 = [1, 2, 4, 5]

const merge = (a, b) => {
  let res = []
  let i = 0, j = 0
  while (i < a.length && j < b.length) {
    if (a[i] < b[j]) {
      res.push(a[i])
      i++
    } else {
      res.push(b[j])
      j++
    }
  }

  if (i < a.length) {
    res.push(...a.slice(i))
  } else {
    res.push(...b.slice(j))
  }

  return res
}

console.log(merge(arr1, arr2));


